package com.atguigu;

import com.atguigu.pojo.User;
import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * projectName: com.atguigu
 *
 * @author: 赵伟风
 * description:
 */
public class MinioDemo {

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        //创建对象，并且属性赋值
//        User user = new User();
//        user.setName("xx");
//        user.setAge(18);

//        User user =  User.builder().name("xx").age(18).build();

        String username = "minioadmin";
        String password = "minioadmin";
        String endpoint = "http://47.94.86.115:9000";
        String bucketName = "java1229minio";
        //1.登录
        MinioClient minioClient = MinioClient.builder().
                endpoint(endpoint).credentials(username, password).build();
        //2.判断桶是否存在
        boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        //3.不存在创建桶，修改权限
        if (!bucketExists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            //设置hello-minio桶的访问权限
            String policy = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::java1229minio/*"
                          } ],
                          "Version" : "2012-10-17"
                        }""";
           minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
        }
        //4.上传对象文件
        minioClient.uploadObject(UploadObjectArgs.
                builder()
                        .bucket(bucketName)
                        .object("sg.png")
                        .filename("d:\\sg.png")
                .build());
        //5.拼接访问地址
        //地址 endpoint + 桶 + 对象名
        //String url = endpoint + "/" + bucketName + "/" + "sg.png";
        String url = String.join("/",endpoint,bucketName,"sg.png");
        System.out.println("url = " + url);


    }
}
